package node;

import lexer.LexType;
import parser.Parser;

import java.io.IOException;

public class ConstDefNode extends ASTNode {
    public final String name = "<ConstDef>";
    // ConstDef → Ident [ '[' ConstExp ']' ] '=' ConstInitVal
    private final String identNode;
    private int lineno;
    private final ConstExpNode constExpNode;
    private final ConstInitValNode constInitValNode;

    public ConstDefNode(String identNode, ConstExpNode constExpNode, ConstInitValNode constInitValNode) {
        this.identNode = identNode;
        this.constExpNode = constExpNode;
        this.constInitValNode = constInitValNode;
    }

    @Override
    public void show() throws IOException {
        Parser.parserWriter.write(LexType.IDENFR + " " + identNode + "\n");

        if (constExpNode != null) {
            Parser.parserWriter.write(LexType.LBRACK + " " + "[" + "\n");
            constExpNode.show();
            Parser.parserWriter.write(LexType.RBRACK + " " + "]" + "\n");
        }

        Parser.parserWriter.write(LexType.ASSIGN + " " + "=" + "\n");
        constInitValNode.show();
        Parser.parserWriter.write(name + "\n");
    }

    public ConstExpNode getConstExpNode() {
        return constExpNode;
    }

    public String getIdentNode() {
        return identNode;
    }

    public int getLineno() {
        return lineno;
    }

    public ConstInitValNode getConstInitValNode() {
        return constInitValNode;
    }

    public void setLineno(int lineno) {
        this.lineno = lineno;
    }

    public boolean isArray() {
        return constExpNode != null;
    }

}
